﻿Namespace Forms.Edit

    <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
    Public Class frmPeriodClassDetails
        Inherits org.codegen.win.controls.frmBaseEdit

#Region "Designer"
        'Form overrides dispose to clean up the component list.
        <System.Diagnostics.DebuggerNonUserCode()> _
        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            Try
                If disposing AndAlso components IsNot Nothing Then
                    components.Dispose()
                End If
            Finally
                MyBase.Dispose(disposing)
            End Try
        End Sub

        Friend WithEvents UcPeriodClass As ucPeriodClassDetails
        Friend WithEvents UcPeriodClassExamDetails As IIC.ucPeriodClassExamDetails
        Friend WithEvents UcGradeMappingList As IIC.ucGradeMappingList

        'Required by the Windows Form Designer
        Private components As System.ComponentModel.IContainer

        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        <System.Diagnostics.DebuggerStepThrough()> _
        Private Sub InitializeComponent()
            Me.UcPeriodClassExamDetails = New IIC.ucPeriodClassExamDetails()
            Me.UcPeriodClass = New IIC.ucPeriodClassDetails()
            Me.UcGradeMappingList = New IIC.ucGradeMappingList()
            Me.SuspendLayout()
            '
            'UcEditToolar
            '
            Me.UcEditToolar.ShowAdd = True
            Me.UcEditToolar.ShowDelete = True
            Me.UcEditToolar.ShowNavigationButtons = True
            Me.UcEditToolar.ShowSaveAs = True
            Me.UcEditToolar.Size = New System.Drawing.Size(797, 30)
            '
            'UcPeriodClassExamDetails
            '
            Me.UcPeriodClassExamDetails.Dock = System.Windows.Forms.DockStyle.Bottom
            Me.UcPeriodClassExamDetails.isInitialized = False
            Me.UcPeriodClassExamDetails.Location = New System.Drawing.Point(0, 308)
            Me.UcPeriodClassExamDetails.ModelObject = Nothing
            Me.UcPeriodClassExamDetails.Name = "UcPeriodClassExamDetails"
            Me.UcPeriodClassExamDetails.Size = New System.Drawing.Size(797, 106)
            Me.UcPeriodClassExamDetails.TabIndex = 2
            '
            'UcPeriodClass
            '
            Me.UcPeriodClass.AutoValidate = System.Windows.Forms.AutoValidate.EnableAllowFocusChange
            Me.UcPeriodClass.Dock = System.Windows.Forms.DockStyle.Top
            Me.UcPeriodClass.isInitialized = False
            Me.UcPeriodClass.Location = New System.Drawing.Point(0, 30)
            Me.UcPeriodClass.ModelObject = Nothing
            Me.UcPeriodClass.Name = "UcPeriodClass"
            Me.UcPeriodClass.Size = New System.Drawing.Size(797, 116)
            Me.UcPeriodClass.TabIndex = 0
            '
            'UcGradeMappingList
            '
            Me.UcGradeMappingList.Dock = System.Windows.Forms.DockStyle.Fill
            Me.UcGradeMappingList.Location = New System.Drawing.Point(0, 146)
            Me.UcGradeMappingList.Name = "UcGradeMappingList"
            Me.UcGradeMappingList.Size = New System.Drawing.Size(797, 162)
            Me.UcGradeMappingList.TabIndex = 3
            '
            'frmPeriodClassDetails
            '
            Me.AutoScaleDimensions = New System.Drawing.SizeF(7.0!, 13.0!)
            Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
            Me.ClientSize = New System.Drawing.Size(797, 436)
            Me.Controls.Add(Me.UcGradeMappingList)
            Me.Controls.Add(Me.UcPeriodClassExamDetails)
            Me.Controls.Add(Me.UcPeriodClass)
            Me.Name = "frmPeriodClassDetails"
            Me.ShowAdd = True
            Me.ShowDelete = True
            Me.ShowNavigationButtons = True
            Me.ShowSaveAs = True
            Me.Text = "Class"
            Me.Controls.SetChildIndex(Me.UcEditToolar, 0)
            Me.Controls.SetChildIndex(Me.UcPeriodClass, 0)
            Me.Controls.SetChildIndex(Me.UcPeriodClassExamDetails, 0)
            Me.Controls.SetChildIndex(Me.UcGradeMappingList, 0)
            Me.ResumeLayout(False)
            Me.PerformLayout()

        End Sub


#End Region

#Region "Standard Code"

        Private _period As Period

        ''' <summary>
        ''' Gets/Sets The period we are working on
        ''' </summary>
        ''' <remarks>
        ''' The period is needed and is always read only when adding a new 
        ''' record, since it is loaded from the tree view, where a period is 
        ''' already selected
        ''' </remarks>
        Public Property Period() As Period
            Get
                Return _period
            End Get
            Set(ByVal value As Period)
                _period = value
                If _period IsNot Nothing Then
                    Me.UcPeriodClass.PeriodId.Value = _period.PeriodId
                    Me.UcPeriodClass.PeriodId.ReadOnly = True

                End If

            End Set
        End Property



        Public Overrides Sub DeleteData()

            Dim mapper As PeriodClassDBMapper = New PeriodClassDBMapper()
            mapper.deleteByKey(Me.IdValue)

            'after delete, close the form
            'Note: do not set the dialog result here.  It is handled by the frmBaseEdit, ie do not call Me.DialogResult = Windows.Forms.DialogResult.None

        End Sub

        Public Overrides Sub LoadData()

            Dim perClass As PeriodClass = New PeriodClassDBMapper().findByKey(Me.IdValue)
            Me.UcPeriodClass.ModelObject = perClass
            Me.UcPeriodClass.loadData()
            Me.UcPeriodClassExamDetails.ModelObject = perClass
            Me.UcPeriodClassExamDetails.loadData()
            Me.setRecordLoadedStatus(Me.IdValue)

        End Sub


        Public Overrides Function SaveData() As enumSaveDataResult

            If Me.ValidateChildren() Then
                Me.UcPeriodClass.loadToObject()
                Me.UcPeriodClassExamDetails.loadToObject()

                Dim pcps As New PeriodClassDBUpdateHandler
                pcps.save(CType(Me.UcPeriodClass.ModelObject, PeriodClass))

                Return enumSaveDataResult.SAVE_SUCESS_AND_CLOSE
            Else
                Return enumSaveDataResult.SAVE_FAIL
            End If

        End Function

        Public Overrides Function dataChanged() As Boolean

            Return Me.UcPeriodClass.hasChanges

        End Function

#End Region

        ''' <summary>
        ''' On the load, we need to call "resetLastLoadedValues"
        ''' at the end of the method
        ''' </summary>
        Private Sub frmPeriodClassDetails_Load(ByVal sender As Object, _
                                               ByVal e As System.EventArgs) Handles Me.Load

            Dim isreadonly As Boolean
            'if we are editing a period class, we must set LessonId and PeriodId to read only
            'if there are class members in this class
            If Me.NewRecord = False Then
                Dim pc As PeriodClass = CType(Me.UcPeriodClass.ModelObject, PeriodClass)
                isreadonly = pc.Members.Count > 0
                If pc.GradeMappings.Count = 0 Then
                    Dim pv As Period = pc.Period.getPreviousPeriod(pc.Period.DiplomaSuperTypeId.GetValueOrDefault)
                    
                    'copy from last period
                    For Each pcv As PeriodClass In pv.Classes

                        If pcv.LessonId = pc.LessonId AndAlso pcv.GradeMappings.Count > 0 Then
                            'copy over the grademappings
                            pcv.GradeMappings.ToList.ForEach(Sub(x)
                                                                 pc.AddGradeMapping(CType(x.copy, GradeMapping))
                                                             End Sub)


                            'pc.PassGrade = pcv.PassGrade
                            'pc.MaxGrade = pcv.MaxGrade
                            Exit For
                        End If

                    Next
                End If
                'save period class so that grades are saved to database!
                PeriodClassDataUtils.savePeriodClass(pc)

                Me.UcGradeMappingList.grdGradeMapping.DataSource = pc.GradeMappings
            End If

            If isreadonly = False Then
                AddHandler Me.UcPeriodClass.PeriodId.SelectedValueChanged, _
                       AddressOf periodIdSelectionChanged

                AddHandler Me.UcPeriodClass.LessonId.SelectedValueChanged, _
                       AddressOf LessonIdSelectionChanged
            End If

            Call periodIdSelectionChanged(Nothing, Nothing)
            Call Me.UcPeriodClass.resetLastLoadedValues()

            If Me.Period IsNot Nothing Then
                If Me.Period.DiplomaSuperType.IsExamSessionOnly Then
                    Me.Text = Me.Text.Replace("Class", "Exam Session")
                End If
            End If
        End Sub

        Private Sub periodIdSelectionChanged(ByVal sender As Object, ByVal e As EventArgs)

            'after selection of PeriodId changed, limit the lessons shown 
            'to the ones that do not already exist in CLass table for 
            'the selected period
            If Me.UcPeriodClass.PeriodId.SelectedValue Is Nothing Then
                Me.UcPeriodClass.LessonId.SelectedItem = Nothing
                Me.UcPeriodClass.LessonId.Enabled = False

            Else
                If Me.NewRecord Then
                    Me.UcPeriodClass.LessonId.Enabled = True
                    Dim newPeriodId As Integer = CInt(Me.UcPeriodClass.PeriodId.SelectedValue)
                    Dim per As Period = PeriodDataUtils.findByKey(newPeriodId)
                    If (per Is Nothing) Then
                        Throw New ApplicationException("Can't find period with id:" & newPeriodId)
                    End If

                    'load lessons that are not already in a class for this period
                    'When creating a period, and add class, the lesson added must be a requirement of a 
                    'certificate with the same DiplomaSuperType of the period
                    'Ticket #61
                    Dim lLessSQL As String = "lessonId not in (select lessonid from class where periodid={0})" & _
                        " and lessonId in (SELECT cr.LessonId " & _
                        " FROM CertificationRequirement AS cr INNER JOIN" & _
                        " CertificationType AS ct ON cr.CertificationTypeId = ct.CertificationTypeId " & _
                        " and ct.DiplomaSuperTypeId={1})"

                    Dim lsns As List(Of Lesson) = LessonDataUtils.findList( _
                                lLessSQL, _
                                per.PeriodId, _
                                per.DiplomaSuperTypeId)

                    Me.UcPeriodClass.setLessonsDatasource(lsns)
                Else
                    Me.UcPeriodClass.LessonId.Enabled = False
                End If

            End If

        End Sub

        Private Sub LessonIdSelectionChanged(ByVal sender As Object, ByVal e As EventArgs)

            'after change of lessonid, we retrieve the mappings of the same lesson from a previous 
            'period and copy them to this
            Dim pc As PeriodClass = CType(Me.UcPeriodClass.ModelObject, PeriodClass)

            If Me.UcPeriodClass.LessonId.SelectedValue Is Nothing OrElse _
                    Me.UcPeriodClass.PeriodId.SelectedValue Is Nothing Then

                pc.ClearGradeMappings()
            Else
                Dim p As Period = CType(Me.UcPeriodClass.PeriodId.SelectedItem, BusObjects.Period)
                Dim ls As Lesson = CType(Me.UcPeriodClass.LessonId.SelectedItem, BusObjects.Lesson)
                Dim pv As Period = p

                pv = p.getPreviousPeriod(p.DiplomaSuperTypeId.GetValueOrDefault)

                Do While True
                    'get previous period
                    If (pv Is Nothing) Then
                        Exit Do
                    End If
                    For Each pcv As PeriodClass In pv.Classes
                        'if the lesson of this previous class equals the lesson we are working on
                        If pcv.LessonId = ls.LessonId AndAlso pcv.GradeMappings.Count > 0 Then
                            'copy over the grademappings
                            pc.GradeMappings = pcv.GradeMappings
                            Exit Do
                        End If
                    Next
                    pv = pv.getPreviousPeriod(p.DiplomaSuperTypeId.GetValueOrDefault)
                Loop

            End If

            Me.UcGradeMappingList.grdGradeMapping.DataSource = pc.GradeMappings

        End Sub

    End Class

End Namespace

